package com.ibeeking.found.upms.b.rest.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.b.rest.service.ICpUserRoleService;
import com.ibeeking.found.upms.b.rest.service.ICpUserService;
import com.ibeeking.found.upms.service.common.bo.CpUserRoleBO;
import com.ibeeking.found.upms.service.common.param.TenantUserParam;
import com.ibeeking.found.upms.service.common.param.UserRoleParam;
import com.ibeeking.found.upms.service.common.query.TenantUserPageQuery;
import com.ibeeking.found.upms.service.common.vo.GmcUserPageVO;
import com.ibeeking.found.upms.service.common.vo.TenantUserVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CsUserController控制器
 * @Description 客服台用户管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/cpUser/v1")
public class CpUserController {

    @Resource
    private ICpUserService cpUserServiceImpl;
    @Resource
    private ICpUserRoleService cpUserRoleServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询客服列表")
    @PostMapping(value = "/pageList")
    public Page<GmcUserPageVO> pageList(@RequestBody TenantUserPageQuery query) {
        return cpUserServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "通过名称或手机号模糊查询客服列表")
    @GetMapping(value = "/listByNameOrMobile/{nameOrMobile}")
    public List<TenantUserVO> listByNameOrMobile(@PathVariable("nameOrMobile") String nameOrMobile) {
        return cpUserServiceImpl.listByNameOrMobile(nameOrMobile);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增客服")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) TenantUserParam param) {
        return cpUserServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改客服")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) TenantUserParam param) {
        return cpUserServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询客服信息")
    @GetMapping(value = "/view/{id}")
    public TenantUserVO view(@PathVariable("id") Long id) {
        return cpUserServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "锁定/解锁客服")
    @GetMapping(value = "/lock/{id}/{status}")
    public Boolean lock(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        if (currentUser.getId().equals(id)) {
            throw new BusinessException("禁止锁定/解锁自己");
        }
        return cpUserServiceImpl.lock(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "离职客服")
    @GetMapping(value = "/quit/{id}")
    public Boolean quit(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id) {
        if (currentUser.getId().equals(id)) {
            throw new BusinessException("禁止离职自己");
        }
        return cpUserServiceImpl.quit(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除客服")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id) {
        if (currentUser.getId().equals(id)) {
            throw new BusinessException("禁止删除自己");
        }
        return cpUserServiceImpl.del(id);
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "重置客服密码")
    @GetMapping(value = "/resetPwd/{id}")
    public Boolean resetPwd(@PathVariable("id") Long id) {
        return cpUserServiceImpl.resetPwd(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "通过客服Id修改客服角色信息")
    @PostMapping(value = "/modifyByUserId")
    public Boolean modifyByUserId(@LoginUser CurrentUser currentUser, @RequestBody @Validated UserRoleParam userRoleParam) {
        return cpUserRoleServiceImpl.modifyUserRoleByUserId(currentUser, userRoleParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "通过客服查询客服拥有角色id列表")
    @GetMapping(value = "/queryRoleIdsByUserId/{userId}")
    public List<String> queryRoleIdsByUserId(@PathVariable("userId") @NotNull(message = "userId不能为空") Long userId) {
        List<CpUserRoleBO> csUserRoleBos = cpUserRoleServiceImpl.queryRoleIdsByUserId(userId);
        return csUserRoleBos.stream().map(s -> String.valueOf(s.getRoleId())).collect(Collectors.toList());
    }
}
